package org.john.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;
import com.googlecode.protobuf.format.JsonFormat;

import java.io.*;

/**
 * Created by adsame on 17-3-20.
 */
public class ByteStringTest {


    public static void main(String args[]) {
        TestProtobuf.BidRequest.Builder builder = TestProtobuf.BidRequest.newBuilder();
        builder.setId("2");
        builder.setIsTest(true);
        builder.addPageKeyword("aha");
        TestProtobuf.BidRequest bidRequest = builder.build();
        byte[] result = bidRequest.toByteArray();
        String output = bidRequest.toString();
        System.out.println(output);
        File file = new File("log/log.txt");
        try {
//            FileOutputStream fos = new FileOutputStream(file);
//            fos.write(output);
//            FileInputStream fis = new FileInputStream(file);

            TestProtobuf.BidRequest serizial = TestProtobuf.BidRequest.parseFrom(result);
//            TestProtobuf.BidRequest serizial = TestProtobuf.BidRequest.parseFrom(output.getBytes());
            System.out.println("seri:\n" + serizial.toString());
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String jsonFormat = JsonFormat.printToString(bidRequest);
        System.out.println(jsonFormat);
        jsonFormat = "{\"id\": \"6\",\"page_keyword\": [\"dog\"],\"is_test\": false}\n";
        try {
            JsonFormat.merge(jsonFormat, builder);
            System.out.println(builder.build());
        } catch (JsonFormat.ParseException e) {
            e.printStackTrace();
        }
    }
}
